class TreeNode {
    constructor(val) {
        this.val = val;
        this.left = null;
        this.right = null;
    }
}

class BinaryTree {
    constructor(arr) {
        this.root = this.createBinaryTree(arr);
    }

    // 通过层序遍历创建二叉树
    createBinaryTree(arr) {
        if (!arr.length) return null;

        let root = new TreeNode(arr[0]);
        let queue = [root];
        let i = 1;

        while (i < arr.length) {
            let current = queue.shift();

            if (arr[i] !== null) {
                current.left = new TreeNode(arr[i]);
                queue.push(current.left);
            }
            i++;

            if (i < arr.length && arr[i] !== null) {
                current.right = new TreeNode(arr[i]);
                queue.push(current.right);
            }
            i++;
        }

        return root;
    }

    // 查找节点
    findNode(val) {
        function dfs(node) {
            if (!node) return null;
            if (node.val === val) return node;
            return dfs(node.left) || dfs(node.right);
        }
        return dfs(this.root);
    }
}
module.exports = BinaryTree;
// 使用示例
// const arr = [3,5,1,6,2,0,8,null,null,7,4];
// const tree = new BinaryTree(arr);

// const node6 = tree.findNode(6);
// const node8 = tree.findNode(8);

// console.log("找到节点:", node6, node8);
